home *** CD-ROM | disk | FTP | other *** search
/ Macwelt 1 / Macwelt DVD 1.toast / Web-Publishing / HTML-Editoren / Alpha ƒ / Mode Examples / Scilab-Example.sci < prev    next >
Encoding:
Text File  |  2000-10-30  |  9.8 KB  |  274 lines

  1. // Scilab-Example.sci
  2. //
  3. // Included in the Alpha distribution as an example of the Scil mode.
  4. // Scil mode is part of the standard Alpha distribution.
  5. //
  6. // Scilab is a scientific software package for numerical computations in a 
  7. // user-friendly environment.  It features:
  8. // 
  9. // Elaborate data structures (polynomial, rational and string matrices, lists,
  10. //     multivariable linear systems,...).
  11. // Sophisticated interpreter and programming language with Matlab-like syntax. 
  12. // Hundreds of built-in math functions (new primitives can easily be added). 
  13. // Stunning graphics (2d, 3d, animation). 
  14. // Open structure (easy interfacing with Fortran and C via online dynamic link). 
  15. // Many built-in libraries: 
  16. //                                        
  17. //    •    Linear Algebra (including sparse matrices, Kronecker form, 
  18. //     ordered Schur,...). 
  19. //    •    Control (Classical, LQG, H-infinity,...). 
  20. //    •    Package for LMI (Linear Matrix Inequalities) optimization. 
  21. //    •    Signal processing. 
  22. //    •    Simulation (various ode's, dassl,...). 
  23. //    •    Optimization (differentiable and non-differentiable, LQ solver). 
  24. //    •    Scicos, an interactive environment for modeling and simulation of 
  25. //     hybrid systems. 
  26. //    •    Metanet (network analysis and optimization). 
  27. //                                 
  28. // Symbolic capabilities through Maple interface. 
  29. // Parallel Scilab. 
  30. // 
  31. // Additional information about Scilab (the source of this file) can be found at:
  32. // 
  33. //     <http://www-rocq.inria.fr/scilab/>
  34. //
  35.  
  36. function [path] = PathToCmapUtility()
  37.   // put here the path to your CmapUtility directory 
  38.   path = ""
  39.   
  40. function [fichier] = DefaultCmapFile()
  41.   path =  PathToCmapUtility()
  42.   fichier = path+"CmapForms.cmp" 
  43.   
  44. function [cmap] = colormap(a1,        a2,     a3)
  45.   //                      (cmap_name, nb_col, cmap_file)
  46.   //
  47.   //  The main function to get a colormap from a Cmap forms file
  48.   //
  49.   //  cmap_name : (string) the name of a cmap (form)
  50.   //  nb_col    : (integer) the nb of color for the resulting cmap
  51.   //  cmap_file : (string) file to read so as to find the cmap_name cmap (form)
  52.  
  53.   //  authorized calling sequences :
  54.   //  (cmap_name), (cmap_name, nb_col), (cmap_name, nb_col, cmap_file) 
  55.   default_nb_col = 64 ;
  56.  
  57.   nb_col = default_nb_col ; cmap_file =  DefaultCmapFile()
  58.   [lhs ,rhs] = argn(0)
  59.  
  60.   select rhs     // some cautions for bad calling (but not complete... in
  61.                  // particular it is assumed that all arg are scalars)
  62.   case 1
  63.     if typeof(a1) == "string" then
  64.       cmap_name = a1
  65.     else
  66.       error(" colormap : first argument may be a string (colormap name)")      
  67.     end
  68.   case 2
  69.     if typeof(a1) == "string" & typeof(a2) == "constant" then
  70.       cmap_name = a1; nb_col = a2
  71.     else
  72.       error(" colormap : arg 1 may be a string (colormap name) and arg 2 an integer (nb_colors) ")
  73.     end
  74.   case 3
  75.     if typeof(a1) == "string" & typeof(a2) == "constant" & typeof(a3) == "string" then
  76.       cmap_name = a1; nb_col = a2, cmap_file = a3
  77.     else
  78.       error(" colormap : arg1 may be a string, arg 2 an integer and arg 3 a string ")
  79.     end
  80.   else      
  81.     error(" colormap : bad calling sequence ")
  82.   end          
  83.  
  84.   [ListCmapf, info] = ReadCmapFormsFile(cmap_file)
  85.   if ~info then
  86.     error(" Problem to read the colormap file "+cmap_file)
  87.   end
  88.   
  89.   trouve = %f ; i = 1
  90.   while ~trouve & (i <= length(ListCmapf))
  91.     if ListCmapf(i).name == cmap_name then
  92.       cmapf =  ListCmapf(i) ; trouve = %t
  93.     else
  94.       i = i+1
  95.     end
  96.   end
  97.   
  98.   if trouve then
  99.     cmap = MakeCmap(cmapf, nb_col)
  100.   else
  101.      write(%io(2)," This colormap doesn''t exist in the file "+cmap_file)
  102.      write(%io(2)," which contains only the following :")
  103.      for i=1:length(ListCmapf)
  104.        write(%io(2),"       "+ListCmapf(i).name)
  105.      end
  106.      cmap = []
  107.    end
  108.     
  109. function [cmap] = CmapUtil(CurrentFile)  
  110.   //
  111.   //   The main function to view and manage some colormaps (forms) files 
  112.   //   and then to build colormaps...
  113.   //   You may also retrieve a scilab colormap (cmap) (a (nc,3) matrix)
  114.   //
  115.   [lhs ,rhs] = argn(0)
  116.   if rhs == 0 then
  117.     CurrentFile = DefaultCmapFile()
  118.   end
  119.   
  120.   path = PathToCmapUtility()
  121.   getf(path+"CmapUtilInternals.sci")  // get all the other funcs
  122.   
  123.   nb_colors = 128
  124.   CmapForm_InitRand()
  125.   //-----------------get a free graphical win and initialise it--------
  126.   win = a_free_gwin()
  127.   xset("window",win)
  128.   xbasc()
  129.   delmenu(win,"File") ; delmenu(win,"2D Zoom") ; delmenu(win,"UnZoom")
  130.   delmenu(win,"3D Rot.")
  131.  
  132.   //------------------add menus and buttons---------------------------
  133.   addmenu(win,"Previous")
  134.   addmenu(win,"Next")
  135.   addmenu(win,"Create", [ "From this colormap form" ;...
  136.                           "From nothing with a little help of rand" ;...
  137.                           "By transform a colormap in a cmap form "])            
  138.   addmenu(win,"Redraw")
  139.   addmenu(win,"Quit")  
  140.   ajoute_menu_Operations(win,CurrentFile)
  141.  
  142.   //------------------define actions associated with menus-----------------
  143.   swin = string(win)
  144.   execstr("Previous_"     + swin + '= ""CurrentAction = """"previous""""""')
  145.   execstr("Next_"        + swin + '= ""CurrentAction = """"next""""""') 
  146.   execstr("Create_"      + swin + '=[""CurrentAction = """"create1"""""" ;...
  147.                                      ""CurrentAction = """"create2"""""" ;...
  148.                                      ""CurrentAction = """"create3""""""]')
  149.   execstr("Operations_"  + swin + '=[""CurrentAction = """"marksd"""""" ;...
  150.                                      ""CurrentAction = """"ch_name"""""";...
  151.                                      ""CurrentAction = """"save1""""""  ;...
  152.                                      ""CurrentAction = """"save2""""""  ;...
  153.                                      ""CurrentAction = """"openf""""""  ;...
  154.                                      ""CurrentAction = """"ch_nbcol""""""]')
  155.   execstr("Redraw_"      + swin + '= ""CurrentAction = """"redraw""""""')
  156.   execstr("Quit_"        + swin + '= ""CurrentAction = """"quit""""""')
  157.   CurrentAction = "readCurrentFile"
  158.  
  159.   while %t    //---- (big...) events loop--------------------------------------
  160.     select CurrentAction
  161.     case "readCurrentFile"
  162.       [ListCmapf, info] = ReadCmapFormsFile(CurrentFile)
  163.       if ~info then
  164.         cmapf =[]
  165.         disp(" Problems to read the file "+CurrentFile+" ...")
  166.         break
  167.       end
  168.       NbCmapf = length(ListCmapf) ; NbCmapf_init = NbCmapf ; Num = 1
  169.       mark = bool2s(ones(1,NbCmapf))   // to mark save/delete
  170.       CurrentAction = "redraw"
  171.     case "next"
  172.        Num = Num + 1 ; if (Num > NbCmapf) then, Num = 1, end
  173.        CurrentAction = "redraw"
  174.     case "previous"
  175.        Num = Num - 1 ; if (Num < 1) then, Num = NbCmapf, end
  176.        CurrentAction = "redraw"
  177.     case "create1"
  178.        [cmapf] = Define_Cmap(ListCmapf(Num)) ; CurrentAction = "insert_in_list"
  179.     case "create2"
  180.        [cmapf] = Define_Cmap() ; CurrentAction = "insert_in_list"
  181.     case "create3"
  182.        [cm, OK] = get_the_cmap()
  183.        if OK then
  184.          cmapf = cmap_to_cmapf(cm) 
  185.          [cmapf] = Define_Cmap(cmapf) ; CurrentAction = "insert_in_list"
  186.        else
  187.          CurrentAction = "nothing"
  188.        end
  189.     case "insert_in_list"
  190.        NbCmapf = NbCmapf + 1 ; Num = NbCmapf ; mark(Num) = %t
  191.        ListCmapf(Num) = cmapf ; ListCmapf(Num).name = "A new with no name"
  192.        CurrentAction = "redraw" ; xset("window",win)  // to set win as the new current gr win
  193.     case "ch_nbcol"
  194.        [nb_colors] = Choix_nb_colors("New number of colors (for colorbar) :", nb_colors)
  195.        CurrentAction = "redraw"
  196.     case "ch_name"
  197.        ListCmapf(Num).name = Get_a_Name("Choose a new name for "+ListCmapf(Num).name)
  198.        CurrentAction = "redrawlight"
  199.     case "marksd"     
  200.        mark(Num) = ~mark(Num)
  201.        CurrentAction = "redrawlight"
  202.     case "save1" 
  203.        FileName = CurrentFile ; CurrentAction = "save"
  204.     case "save2"
  205.        FileName = Get_a_Name("Give a file name") ; CurrentAction = "save"
  206.     case "save"
  207.        [ListCmapf] = saveCmapf(ListCmapf, mark, FileName)
  208.        Num = 1; NbCmapf = length(ListCmapf) ; NbCmapf_init = NbCmapf
  209.        mark = bool2s(ones(NbCmapf,1))
  210.        CurrentAction = "redraw"
  211.     case "openf"
  212.        CurrentFile = xgetfile("*.cmp",title="Choose a Colormap Forms file")
  213.        delmenu(win,"Operations") ; ajoute_menu_Operations(win,CurrentFile)
  214.        CurrentAction = "readCurrentFile"
  215.     case "redraw"
  216.        [cmap, red, green, blue] = ActualiseCmap(ListCmapf(Num), nb_colors)
  217.        CurrentAction = "redrawlight"
  218.     case "redrawlight"
  219.        xbasc()
  220.        xsetech([0 0 1 1],[0 -0.5 1 1])
  221.        PlotLaTotale(ListCmapf(Num), nb_colors, red, green, blue)
  222.        disp_info(ListCmapf(Num).name, mark(Num), NbCmapf, NbCmapf_init, Num)
  223.        CurrentAction = "nothing"
  224.     case "quit"
  225.        cmapf = ListCmapf(Num)
  226.        nb_col = Choix_nb_colors("Choose a number of colors for the selected cmap form", 64)
  227.        cmap = MakeCmap(cmapf, nb_col)    
  228.        break
  229.     end
  230.   end //--- of the (big) events loop-----------------------------------------/
  231.   
  232.   xset("default")
  233.   xdel(win)
  234.  
  235.   
  236. function [ListCmapf, info] = ReadCmapFormsFile(FileName)
  237.   [unit, err] = file("open", FileName, "old")
  238.   ListCmapf = list()
  239.   if err < 1 then
  240.      info = %t
  241.      n = read(unit,1,1)
  242.      for i=1:n
  243.         ListCmapf(i) = tlist(["CmapForm", "name", "red", "green", "blue"])
  244.         ListCmapf(i)("name") = read(unit,1,1,"(a)")
  245.         for j=3:5
  246.            l = read(unit,1,1)
  247.            x = read(unit,1,l) ; y =  read(unit,1,l)
  248.            ListCmapf(i)(j) = tlist(["ColorIntensity","x","y"], x, y)
  249.         end
  250.      end
  251.      file("close",unit)
  252.   else
  253.      info = %f
  254.   end
  255.   
  256. function [cmap] = MakeCmap(cmapf, nb_colors)
  257.   //   Get a colormap cmap from a colormap form cmapf and
  258.   //   a number of colors nb_colors
  259.   x = linspace(0,1,nb_colors)'
  260.   cmap = [ cmapf("red")*x , cmapf("green")*x , cmapf("blue")*x ]
  261.  
  262. function [yy] = %ColorInt_m_s(color, xx)
  263.   //   Definition of the operation ColorIntensity * vector
  264.   yy = interpln([color.x ; color.y],xx)'
  265.   yy = min(1, max(0 , yy))
  266.   
  267.  
  268.  
  269.  
  270.  
  271.  
  272.  
  273.  
  274.